<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: {2021/5/22}
 * Time: {20:38}
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\common\business;


use think\Exception;
use think\facade\Log;

class BusBase
{
    //是否验证允许重复
    protected bool $repeat = false;
    //验证数据库重复字段
    protected string $repeat_string = '';

    /**
     * 添加方法
     * @param array $data
     * @return bool
     * @throws Exception
     */
    public function add(array $data = []): bool
    {
        if (empty($data)) {
            throw new Exception("数据不完整");
        }
        $this->valAdd($data);
        //dump($data);exit();
        //添加入库
        try {
            $res = $this->modelObj->insertDataByInsertId($data);
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("入库失败");
        }
        if ($res) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param $data
     * @throws Exception
     */
    public function valAdd($data)
    {
        //验证
        if (!$this->validateObj->scene('add')->check($data)) {
            throw new Exception($this->validateObj->getError());
        }
        //验证是否可以重复
        if ($this->repeat) {
            if (empty($this->repeat_string)) {
                throw new Exception("值不能为空");
            }
            $array = explode(',', $this->repeat_string);
            $map = [];
            foreach ($array as $k => $v) {
                $map[$k] = [
                    [$v, '=', $data[$v]]
                ];
            }
            try {
                $res = $this->modelObj->getBaseRepeat($map);
            } catch (\Exception $e) {
                throw new Exception("操作失败");
            }
            if ($res) {
                throw new Exception("请不要重复添加");
            }
        }
        //dump($data);exit();
    }

    /**
     * 获取后台首页
     * @param array $map
     * @param int $limit
     * @return array
     * @throws Exception
     */
    public function getIndexPage(array $map = [], int $limit = 10)
    {
        try {
            $result = $this->modelObj->getIndexPage($map, $limit);
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("发生错误");
        }
        if ($result) {
            return $result;
        } else {
            return [];
        }
    }

    /**
     * 根据条件更新
     * @param array $data
     * @return bool
     * @throws Exception
     */
    public function updateEditValue(array $data = [])
    {
        if (empty($data)) {
            return false;
        }
        if (!$this->validateObj->scene('update')->check($data)) {
            throw new Exception($this->validateObj->getError());
        }
        try {
            $res = $this->modelObj->updateById($data['id'], [$data['field'] => $data['value']]);
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("更新失败");
        }
        if ($res) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param array $data
     * @return false
     * @throws Exception
     */
    public function addAll(array $data = [])
    {
        try {
            $result = $this->modelObj->addAll($data);
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("发生错误");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return false;
        }
    }

    /**
     * @param int $id
     * @param array $data
     * @return bool
     * @throws Exception
     */
    public function updateById(int $id = 0, array $data = []): bool
    {
        try {
            $result = $this->modelObj->updateById($id, $data);
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("更新失败");
        }
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
}